<?php
require_once 'Zend/Controller/Action.php';
require_once 'MF/Vote.inc';

class Cms_VotesController extends Zend_Controller_Action {
    protected $_main;
    protected $_session;
    function init(){
		CmsAccess::restrictModule('voite');
		
        $this->_main = MF_Main::getInstance();
        $this->_session = new Zend_Session_Namespace;        
        $this->view->setHelperPath('core/helpers/'); 
    }

    public function indexAction(){
        $vote = MF_Main::getInstance()->Vote;
        $this->view->vote = $vote;
    }
    
    public function editAction(){
      	$filter = new Zend_Filter_StripTags();

        $votes = MF_Main::getInstance()->Vote;
        $vote_id = $filter->filter($this->_getParam('id'));    
        
        $f = false;
        if( $vote_id == -1 ){ //пришли по кнопке "Добавить опрос"
            $vote = $votes->add();    //создаем новый опрос
            $vote_id = $vote->Vote_id;
            $f = true;
        }else{        
            $vote = $votes->GetById($vote_id); //будем править существующий
        }
        $vote->Make();

        if( isset($_POST['okButton']) ){//нажали кнопку "Сохранить"
            //основные данные
            $vote->Question = $filter->filter($_POST['question']);
            $vote->Status_id = $filter->filter($_POST['status']);
            $vote->Work_until = $filter->filter($_POST['expire_date']);
            //варианты ответов
            if( isset($_POST['answer']) ){
                foreach ($_POST['answer'] as $index => $a){
                    $ans = $vote->getById($filter->filter($index));
                    $ans->Answer = $filter->filter($a);
                }            
            }
        }
        //данные по опросу
        $this->view->vote_data = array();
        //общие
        $this->view->vote_data['vote_id'] = $vote_id;
        $this->view->vote_data['question'] = $vote->Question;
        $this->view->vote_data['status'] = $vote->Status_id;
        $this->view->vote_data['work_until'] = $vote->Work_until;
        //варианты ответов
        $c = $vote->length();
        $vote->Make();
        $this->view->vote_data['answers'] = array();
        for($i=0; $i<$c; $i++){
            $a = $vote->item($i);
            $this->view->vote_data['answers'][] = 
                array('id' => $a->Answer_id, "answer" => $a->Answer, "sequence" => $a->Sequence, "votes" => $a->votes);
        }        
        
        //если создавали отчет, то генерим нормальный адрес для него
        if( $f )
            $this->_redirect("/cms/votes/edit/id/$vote_id"); 
    }

    public function addanswerAction(){
    //добавляем новый вариант ответа и тут же возвращаемся обратно
        $filter = new Zend_Filter_StripTags();
        $vote_id = $filter->filter($this->_getParam('id'));    

        $votes = MF_Main::getInstance()->Vote;
        $vote = $votes->GetById($vote_id);
        $vote->add("");
        
        $this->_redirect($_SERVER["HTTP_REFERER"]);  	
    }

    public function delanswerAction(){
    //удалить один вариант ответа
        $filter = new Zend_Filter_StripTags();
        $answer_id = $filter->filter($this->_getParam('id'));    

        //удалим просто так
        //сначало сам ответ
        echo "удаляем $answer_id: <br/>";
        MF_Main::getInstance()->_sql->exeDummy("delete from ".TAB_PREF."vote_answer where Answer_id=".$answer_id);	        
        //а тут всех проголосовавших
        echo "2<br />";
        MF_Main::getInstance()->_sql->exeDummy("delete from ".TAB_PREF."vote_history where Answer_id=".$answer_id);	            
        echo "3<br />";
        
        $this->_redirect($_SERVER["HTTP_REFERER"]);  	
    }

    public function delvoteAction(){
        $filter = new Zend_Filter_StripTags();
        $vote_id = $filter->filter($this->_getParam('id'));    

        $votes = MF_Main::getInstance()->Vote;
        $vote = $votes->GetById($vote_id);
        $vote->delete();
        
        $this->_redirect($_SERVER["HTTP_REFERER"]);  	
    }
}